<template>
	<view class="merchant">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				设备铺货
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}">
			<view class="nr clearfix">
				<view class="list">
					<image :src="baseUrl + '/wximage/kongyuan.png'"></image>
					<view class="shuzi">1</view>
					<view class="title">新增商户</view>
				</view>
				<view class="list">
					<image :src="baseUrl + '/wximage/shiyuan.png'"></image>
					<view class="shuzi">2</view>
					<view class="title">新建门店</view>
				</view>
				<view class="list">
					<image :src="baseUrl + '/wximage/kongyuan.png'"></image>
					<view class="shuzi">3</view>
					<view class="title">开通分成</view>
				</view>
				<view class="list">
					<image :src="baseUrl + '/wximage/kongyuan.png'"></image>
					<view class="shuzi">4</view>
					<view class="title">完成新增</view>
				</view>
			</view>
		</view>
		<view class="upload">
			<image :src="baseUrl + '/wximage/shangchuanbj.png'" @click="chooseImage" v-if="formData.sitepicture==''" class="bj"></image>
			<image :src="formData.sitepicture" @click="chooseImage" v-else class="bj"></image>
		</view>
		<view class="form">
			<view class="content clearfix">
				<view class="name fl">门店名称</view>
				<view class="inp fr">
					<input type="text" v-model="formData.name" placeholder="请输入门店名称" />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">门店类型</view>
				<view class="xuanze fr" @click="lxstatus = true">
					{{lxlabel}}
				</view>
				<u-select v-model="lxstatus" :list="lxlist" @confirm="confirmlx"></u-select>
			</view>
			<view class="content clearfix">
				<view class="name fl">联系人</view>
				<view class="inp fr">
					<input type="text" v-model="formData.lxname" placeholder="请输入联系人" />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">手机号码</view>
				<view class="inp fr">
					<input type="text"  v-model="formData.phone" placeholder="请输入手机号码" maxlength="11" />
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">省市区</view>
				<view class="xuanze fr" @click="getLocation">
					<text v-if="formData.province_text==''">请选择</text>
					<text v-else>{{formData.province_text}}{{formData.city_text}}{{formData.district_text}}</text>
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">详细地址</view>
				<view class="inp fr">
					<input type="text" placeholder="请输入详细地址" v-model="formData.full_address" />
				</view>
			</view>
			<view class="content contentlast clearfix">
				<view class="name fl">营业时间</view>
				<view class="xuanze fr">
					<picker :range="years" @change="yearChange" mode="multiSelector">
						{{formData.yyshijian}}
					</picker>
				</view>
			</view>
		</view>
		<view class="shoufei">
			<view class="fangan">
				<view class="top clearfix">
					<view class="title fl">计费策略（普通宝）</view>
					<view class="edit fr" @click="billingUpdate">修改</view>
				</view>
				<view class="bottom">
					<view class="text">前{{billing.mianfei}}分钟免费丨每{{billing.minute}}{{billing.attr==1?'分钟':'小时'}}：¥ {{billing.danjia}}</view>
					<view class="text">单日封顶：¥ {{billing.dayconsume}} 丨总封顶：¥{{billing.maxmoney}}</view>
				</view>
			</view>
			<view class="fangan">
				<view class="top clearfix">
					<view class="title fl">计费策略（快充宝）</view>
					<view class="edit fr" @click="billingUpdate">修改</view>
				</view>
				<view class="bottom">
					<view class="text">前{{kbilling.mianfei}}分钟免费丨每{{kbilling.minute}}{{kbilling.attr==1?'分钟':'小时'}}：¥ {{kbilling.danjia}}</view>
					<view class="text">单日封顶：¥ {{kbilling.dayconsume}} 丨总封顶：¥{{kbilling.maxmoney}}</view>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="submit" @click="submit">下一步</view>
		</view>
	</view>
</template>

<script>
	const captureLocation = (res) => {
		var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
		var province = [];
		var region_province = '';
		var addressBean = {
			province: null,
			country: null,
			city: null,
			address: null,
		};
	
		function regexAddressBean(address, addressBean) {
			regex = /^(.*?[市]|.*?[州]|.*?地区|.*?特别行政区)(.*?[区]|.*?[市]|.*?[县])(.*?)$/g;
			var addxress = regex.exec(address);
			addressBean.city = addxress[1];
			addressBean.country = addxress[2];
			addressBean.address = addxress[3] + '(' + res.name + ')';
		}
		if (!(province = regex.exec(res.address))) {
			regex = /^(.*?(省|自治区))(.*?)$/;
			region_province = regex.exec(res.address);
			addressBean.province = region_province[1];
			regexAddressBean(region_province[3], addressBean);
		} else {
			addressBean.province = region_province[1];
			regexAddressBean(res.address, addressBean);
		}
		return addressBean;
	}
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				lxstatus: false,
				lxlist: [],
				jsvalue: 0,
				lxlabel: '请选择',
				address: '',
				latitude: '',
				longitude: '',
				province: '',
				city: '',
				district: '',
				provincetitle: '',
				citytitle: '',
				areatitle: '',
				formData:{
					name: '',
					full_address: '',
					yyshijian: '请选择',
					store_type: '',
					mechartid: '',
					sitepicture: '',
					billingstatus: 0 ,//普通宝 系统默认0  门店计费方式id
					kbillingstatus: 0, //快充饱系统默认0  门店计费方式id
					lat: '',
					lng: '',
					province_text: '',
					city_text: '',
					district_text: '',
					province: '',
					city: '',
					district: '',
					chargerid: '',
					agentid:'',
					lxname: '',
					phone: '',
				},
				billing: {
					mianfei: "",//免费时长
					minute: '',//时间
					danjia: '',//价格
					dayconsume: '',//每日封顶
					maxmoney: '',//封顶金额
					attr: 1//1分钟 2小时
				},
				kbilling: {
					mianfei: "",//免费时长
					minute: '',//时间
					danjia: '',//价格
					dayconsume: '',//每日封顶
					maxmoney: '',//封顶金额
					attr: 1//1分钟 2小时
				},
				idm:'',
				idk:'',
				years:[
					['00:00','01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'],
					['01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00','24:00']
				],
			}
		},
		onLoad(option) {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			//获取一下传过来的商户id是多少嗷
			// console.log(option.userid);
			this.formData.mechartid = uni.getStorageSync('puhuobussiness_id');
			
			//设备的
			this.formData.chargerid = uni.getStorageSync('puhuosn');
			
			this.formData.agentid = uni.getStorageSync('agent_id');
			
			this.getstorelx();
			this.configBilling1();
			this.configBilling2();
			
		},
		onShow() {
			// this.configBilling();
			//判断一下缓存是否有计费id
			// uni.setStorageSync('billingidm',id);
			this.idm = uni.getStorageSync('deviceidm');
			if(this.idm==null||this.idm==''||this.idm==undefined){
				this.configBilling1();
			}else{
				this.billingInfo(this.idm,1);
			}
			
			this.idk = uni.getStorageSync('deviceidk');
			if(this.idk==null||this.idk==''||this.idk==undefined){
				this.configBilling2();
			}else{
				this.billingInfo(this.idk,2);
			}
		},
		methods: {
			yearChange(e){
				this.formData.yyshijian = this.years[0][e.detail.value[0]] + '--' + this.years[1][e.detail.value[1]]
			},
			billingInfo(id,type){
				this.request({
					url: '/xcxlogin/billinginfo',
					data: {id:id},
					method: "GET"
				}).then(res => {
					if(res.data.code == 200){
						if(type==1){
							this.billing = res.data.data;
						}else{
							this.kbilling = res.data.data;
						}
					}
				});
			},
			configBilling1(){
				//请求普通宝的计费方式
				this.request({
					url: '/webconfig/deviceconfiginfo',
					data: {},
					method: "GET"
				}).then(res => {
					if (res.data.code == 200) {
						this.billing = res.data.data;
					} else {
						uni.showToast({
							title: '请求失败，请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			configBilling2(){
				//请求一下快充宝的计费方式
				this.request({
					url: '/webconfig/deviceconfiginfo1',
					data: {},
					method: "GET"
				}).then(res => {
					if (res.data.code == 200) {
						this.kbilling = res.data.data;
					} else {
						uni.showToast({
							title: '请求失败，请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			//跳转到修改宝计费策略
			billingUpdate(){
				uni.navigateTo({
					url: '/subpackageA/pricing/deviceindex?idm='+this.idm+'&idk='+this.idk
				});
			},
			chooseImage() {
				let that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
					success: function(res) {
						uni.uploadFile({
							url: 'https://aapi.jixiaodian.cn/api/common/cosupdate',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: function(res1) {
								let arr = JSON.parse(res1.data);
								that.formData.sitepicture = arr.data.path;
							},
							fail: function(res1) {
			
							},
						})
					}
				})
			},
			//门店类型
			getstorelx(){
				this.request({
					url: '/xcx/storetype',
					data: {
						
					},
					method: "POST"
				}).then(res => {
					if(res.data.code == 200){
						let list = res.data.data;
						
						for(let i=0;i<list.length;i++){
							this.lxlist.push({
								value: list[i].id,
								label: list[i].name,
							})
						}
					}
				});
			},
			submit(){
				if(this.formData.name==null||this.formData.name==''||this.formData.name==undefined){
					uni.showToast({
						title: '门店名称不能为空',
						icon: 'none'
					})
					return false;
				}
				if(this.formData.full_address==null||this.formData.full_address==''||this.formData.full_address==undefined){
					uni.showToast({
						title: '详细地址不能为空',
						icon: 'none'
					})
					return false;
				}
				if(this.formData.store_type==null||this.formData.store_type==''||this.formData.store_type==undefined){
					uni.showToast({
						title: '门店类型不能为空',
						icon: 'none'
					})
					return false;
				}
				
				if(this.formData.yyshijian==null||this.formData.yyshijian==''||this.formData.yyshijian==undefined||this.formData.yyshijian=='请选择'){
					uni.showToast({
						title: '请选择营业时间',
						icon: 'none'
					})
					return false;
				}
				
				if(this.formData.province_text==null||this.formData.province_text==''||this.formData.province_text==undefined){
					uni.showToast({
						title: '请选择省市区',
						icon: 'none'
					})
					return false;
				}
				
				if(this.formData.lxname==null||this.formData.lxname==''||this.formData.lxname==undefined){
					uni.showToast({
						title: '联系人不能为空',
						icon: 'none'
					})
					return false;
				}
				
				var reg = /^1[3456789]\d{9}$/;
				if(!reg.test(this.formData.phone)){
					uni.showToast({
						title: '手机格式不正确',
						icon: 'none'
					})
					return false;
				}
				
				//判断一下是否为空
				if(this.idm==''||this.idm==null){
					this.idm = 0;
				}
				
				if(this.idk==''||this.idk==null){
					this.idk = 0;
				}
				
				
				this.formData.billingstatus = this.idm;
				this.formData.kbillingstatus = this.idk;
				
				this.request({
					url: '/wxdevice/addstore',
					data: this.formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						var stores_id = res.data.data.id;//用户id

						uni.setStorageSync('puhuobuzhou',3);
						uni.setStorageSync('device_stores_id',stores_id);
						uni.redirectTo({
							url: '/subpackageA/device/divide'
						})
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			confirmlx(e) {
				this.jsvalue = e[0].value;
				this.lxlabel = e[0].label;
				this.formData.store_type = e[0].value;
				
			},
			getLocation() {
				uni.chooseLocation({
					success: (res) => {
						this.address = res.address;
						this.formData.full_address = res.address;
						this.latitude = res.latitude;
						this.formData.lat = res.latitude;
						this.longitude = res.longitude;
						this.formData.lng = res.longitude;
						let locationObj = captureLocation(res);
						if(locationObj.province){
							this.provincetitle = locationObj.province;
							this.formData.province_text = locationObj.province;
						}else{
							this.provincetitle = locationObj.city;
							this.formData.province_text = locationObj.city;
						}
						this.citytitle = locationObj.city;
						this.formData.city_text = locationObj.city;
						this.areatitle = locationObj.country;
						this.formData.district_text = locationObj.country;
				
						this.request({
							url: '/xcxlogin/getaddressCode',
							data: {
								code: this.provincetitle
							},
							method: "POST"
						}).then(res1 => {
							if (res1.data.code == 200) {
								this.province = res1.data.data.result[0][0].id;
								this.formData.province = res1.data.data.result[0][0].id;
							} else {
								uni.showToast({
									title: '请求失败，请稍后再试',
									icon: 'none'
								})
							}
						})
				
						this.request({
							url: '/xcxlogin/getaddressCode',
							data: {
								code: this.citytitle
							},
							method: "POST"
						}).then(res1 => {
							if (res1.data.code == 200) {
								this.city = res1.data.data.result[0][0].id;
								this.formData.city = res1.data.data.result[0][0].id;
							} else {
								uni.showToast({
									title: '请求失败，请稍后再试',
									icon: 'none'
								})
							}
						})
				
						this.request({
							url: '/xcxlogin/getaddressCode',
							data: {
								code: this.areatitle
							},
							method: "POST"
						}).then(res1 => {
							if (res1.data.code == 200) {
								this.district = res1.data.data.result[0][0].id;
								this.formData.district = res1.data.data.result[0][0].id;
								this.addressquancheng = res1.data.data.result[0][0].address;
							} else {
								uni.showToast({
									title: '请求失败，请稍后再试',
									icon: 'none'
								})
							}
						})
					}
				});
				
				/* uni.chooseLocation({
					success: (res) => {
						this.address = res.provinceName + res.cityName + res.adName + res.address;
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						this.provincetitle = res.provinceName;
						this.citytitle = res.cityName;
						this.areatitle = res.adName;
				
						this.request({
							url: '/xcxlogin/getaddressCode',
							data: {
								code: this.provincetitle
							},
							method: "POST"
						}).then(res1 => {
							if (res1.data.code == 200) {
								this.province = res1.data.data.result[0][0].id;
							} else {
								uni.showToast({
									title: '请求失败，请稍后再试',
									icon: 'none'
								})
							}
						})
				
						this.request({
							url: '/xcxlogin/getaddressCode',
							data: {
								code: this.citytitle
							},
							method: "POST"
						}).then(res1 => {
							if (res1.data.code == 200) {
								this.city = res1.data.data.result[0][0].id;
							} else {
								uni.showToast({
									title: '请求失败，请稍后再试',
									icon: 'none'
								})
							}
						})
				
						this.request({
							url: '/xcxlogin/getaddressCode',
							data: {
								code: this.areatitle
							},
							method: "POST"
						}).then(res1 => {
							if (res1.data.code == 200) {
								this.district = res1.data.data.result[0][0].id;
								this.addressquancheng = res1.data.data.result[0][0].address;
							} else {
								uni.showToast({
									title: '请求失败，请稍后再试',
									icon: 'none'
								})
							}
						})
					}
				}); */
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.merchant {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;		
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			
			.nr{
				width: 94%;
				margin: 0 auto;
				box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
				border-radius: 20rpx;
				background: #FFFFFF;
				position: relative;
				
				.list{
					width: 25%;
					float: left;
					text-align: center;
					position: relative;
					
					image{
						width: 64rpx;
						height: 64rpx;
						margin-top: 26rpx;
					}
					
					.shuzi{
						font-size: 32rpx;
						color: #FFFFFF;
						font-weight: 700;
						position: absolute;
						top: 36rpx;
						left: 62rpx;
					}
					
					.title{
						font-size: 24rpx;
						color: #333333;
						margin-bottom: 26rpx;
						font-weight: 700;
					}
				}
				
				.hengxian{
					position: absolute;
					width: 490rpx;
					height: 2rpx;
					background: #C5C5C5;
					left: 110rpx;
					top: 56rpx;
				}
			}
		}
		
		.upload{
			width: 94%;
			margin: 0 auto;
			margin-top: -120rpx;
			text-align: center;
			position: relative;
			
			.bj{
				width: 382rpx;
				height: 260rpx;
			}
			
			.sc{
				width: 100rpx;
				height: 100rpx;
				position: absolute;
				top: 56rpx;
				left: 140rpx;
			}
			
		}
		
		.form{
			width: 94%;
			margin: 0 auto;
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 0 20rpx;
			box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.content{
				height: 92rpx;
				line-height: 92rpx;
				border-bottom: 1rpx solid #D8D8D8;
				
				.name{
					font-size: 28rpx;
					color: #333333;
					width: 40%;
				}
				
				.inp{
					width: 60%;
					text-align: right;
					
					input{
						font-size: 28rpx;
						color: #999999;
						height: 92rpx;
						line-height: 92rpx;
					}
				}
				
				.xuanze{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					color: #368BF8;
				}
				
				.img{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					
					image{
						width: 108rpx;
						height: 64rpx;
						margin-top: 12rpx;
					}
				}
			}
			
			.contentlast{
				border: none;
			}
		}
		
		.shoufei{
			width: 94%;
			margin: 0 auto;
			margin-top: 40rpx;
			
			.fangan{
				width: 100%;
				margin: 0 auto;
				margin-bottom: 40rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				padding: 32rpx 20rpx 22rpx 20rpx;
				
				.top{
					padding-bottom: 20rpx;
					border-bottom: 1rpx solid #D8D8D8;
					
					.title{
						font-size: 28rpx;
						color: #333333;
					}
					
					.edit{
						font-size: 28rpx;
						color: #368BF8;
					}
				}
				
				.bottom{
					
					.text{
						font-size: 24rpx;
						color: #333333;
						margin-top: 20rpx;
					}
				}
			}
		}
		
		.footer{
			width: 100%;
			margin: 0 auto 40rpx;
			margin-top: 60rpx;
			height: 72rpx;
			text-align: center;
			
			.submit{
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				margin-left: 248rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
	}
</style>